//
//  WithDrawalViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/5.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "WithDrawalViewModel.h"
#import "WithDrawalView.h"
#import "WithDrawalCommissionView.h"

@interface WithDrawalViewModel ()<UITextFieldDelegate>
{
    NSString *endTextStr; // 输入的数字
    WithDrawalCommissionView *topView;
    UIView *selfView;
}

@property (nonatomic, strong) WithDrawalView *withView;
@end

@implementation WithDrawalViewModel

- (instancetype)initWithSuperView:(UIView *)superView
{
    if (self = [super initWithSuperView:superView]) {
        selfView = superView;
       self.withView = [[WithDrawalView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - kHeightProportion_In_iPhoneTriploid(64)) inSuperView:superView];
        if (iPhoneX) {
           self.withView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - kHeightProportion_In_iPhoneTriploid(34) - kHeightProportion_In_iPhoneTriploid(88));
        }
        self.withView.textField.delegate = self;
        [self.withView.withDrawBtn addTarget:self action:@selector(withDrawAction:) forControlEvents:UIControlEventTouchUpInside];
        kWeakSelf(self);
        self.withView.BlockAllWithDraw = ^(GLGWButton * _Nullable allWithDraw) {
            [weakself allWithDrawAction:allWithDraw];
        };
        self.withView.BlockTapLab = ^(NSString * _Nullable tapStr) {
            [weakself tapStr:tapStr];
        };
    }
    return self;
}

- (void)tapStr:(NSString *)str
{
    if (self.BlockTapLab) {
        self.BlockTapLab(str);
    }
}

- (void)setWithDrawType:(WithDrawType)withDrawType
{
    _withDrawType = withDrawType;
    if (withDrawType == WithDrawType_Commission) {
        self.withView.y += 20;
        self.withView.height -= 20;
        topView = [[WithDrawalCommissionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(30)) inSuperView:selfView];
        kWeakSelf(self);
        topView.BlockDelete = ^(GLGWButton * _Nullable deleteBtn) {
            [weakself deleteTopView:deleteBtn];
        };
    }
}

- (void)deleteTopView:(GLGWButton *)sender
{
    [UIView animateWithDuration:0.3 animations:^{
        self.withView.y -= 20;
        self.withView.height += 20;
        [self->topView removeFromSuperview];
    } completion:nil];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *str = [NSString stringWithTextFiledStr:textField.text replacementString:string];
    [self tipLab:str];
    
    return YES;
}

- (void)tipLab:(NSString *)str
{
    if ([str intValue] < 666 && str > 0) {
        [self.withView tipStr:@"可提现金额666元" colorStr:@"#999999"];
        [self.withView.withDrawBtn setBackgroundColor:[UIColor colorWithHexString:@"#A89A60"]];
//       [view.withDrawBtn.layer addSublayer: [view.withDrawBtn setGradualChangingColorFromColor:@"A89A60" toColor:@"A89A60" toHexColorStrAphla:0.3]];
        self.withView.withDrawBtn.enabled = NO;
    } else {
        [self.withView tipStr:@"金额已超过可提现金额" colorStr:@"#EC5353"];
        [self.withView.withDrawBtn setBackgroundColor:[UIColor colorWithHexString:@"#D8D8D8"]];
        self.withView.withDrawBtn.enabled = NO;
    }
}

- (void)allWithDrawAction:(GLGWButton *)sender
{
    self.withView.textField.text = @"666";
}

- (void)btnAction:(GLGWButton *)sender
{
    
}

- (void)withDrawAction:(GLGWButton *)sender
{
    if (self.BlockWithDraw) {
        self.BlockWithDraw(sender);
    }
}
@end
